package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ServletValidate
 */
@WebServlet("/ServletValidate")
public class ServletValidate extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletValidate() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String u = request.getParameter("uname");
        String p = request.getParameter("pass");
        
		String info1 = "";
		String info2 = "";
		String info3 = "";
	
		if(u != "" && p != ""){
		try{
			Class.forName("org.sqlite.JDBC");
			String url = "jdbc:sqlite:c:/Users/JayeM/workspace/MyWebProject/MyWebProject/WebContent/database/database.db";

			
			String sql = "SELECT username, userpassword, usertype FROM tbluserinfo"; 
			Connection con1 = DriverManager.getConnection(url);
			PreparedStatement pstmt = con1.prepareStatement(sql);
			
			ResultSet rst = pstmt.executeQuery();
			
			while(rst.next()){
				
				info1 = rst.getString(1);
				info2 = rst.getString(2);
				info3 = rst.getString(3);
				
				if(info1.equals(u) && info2.equals(p)){
					
					if (info3.equals("admin")){
						HttpSession session = request.getSession();
						response.sendRedirect("listadmin.jsp");
						
						Cookie cookie = new Cookie("cuser", u);
						cookie.setMaxAge(60*30);
						response.addCookie(cookie);
						
						HttpSession s1 = request.getSession();
				        s1.setMaxInactiveInterval(10);
				        s1.setAttribute("uname", u);
						session.setAttribute("pass", p);
						session.setAttribute("uname", u);
					}
					else {
						HttpSession session = request.getSession();
						response.sendRedirect("listitem.jsp");
						
						Cookie cookie = new Cookie("cuser", u);
						cookie.setMaxAge(60*30);
						response.addCookie(cookie);
						
						HttpSession s1 = request.getSession();
				        s1.setMaxInactiveInterval(1800);
				        s1.setAttribute("uname", u);
						session.setAttribute("pass", p);
						session.setAttribute("uname", u);
					}	
				
				}
				}con1.close();
			
			}catch(Exception e){
				//e.printStackTrace();
			}
		}
    }
		
    
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

}